#include "createprojectdialog.h"
#include "ui_createprojectdialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <Core/Core.hpp>


CreateProjectDialog::CreateProjectDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::CreateProjectDialog)
{
	ui->setupUi(this);
}


CreateProjectDialog::~CreateProjectDialog()
{
	delete ui;
}


const QString CreateProjectDialog::gameName() const
{
	if( ui && ui->GameNameEditor )
	{
		return ui->GameNameEditor->text();
	}
	return QString();
}


const QString CreateProjectDialog::baseDirectory() const
{
	if( ui && ui->BaseDirectoryEditor )
	{
		return ui->BaseDirectoryEditor->text();
	}
	return QString();
}


void CreateProjectDialog::on_SelectBaseDirectory_clicked()
{
	QFileDialog		fileDlg( this );
	fileDlg.setFileMode( QFileDialog::DirectoryOnly );

	if( fileDlg.exec() )
	{
		QStringList	fileList	=	fileDlg.selectedFiles();
		if( fileList.size()==1 )
		{
			DLOG( INFO ) << "Selected directory: " << fileList[ 0 ].toUtf8().data();
			ui->BaseDirectoryEditor->setText( fileList[ 0 ] );
		}
	}
}


void CreateProjectDialog::done( int res )
{
	DLOG( INFO ) << "Done";
	QDialog::done( res );
}


void CreateProjectDialog::accept()
{
	DLOG( INFO ) << "Accept";
	QDir	dir( baseDirectory() );
	if( !dir.exists( baseDirectory() ) )
	{
		QMessageBox	msgBox;
		msgBox.setText( "Base directory does not exist!" );
		msgBox.setInformativeText( "Create it?" );
		msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::Abort );
		msgBox.setDefaultButton( QMessageBox::Yes );

		if( msgBox.exec() )
		{
			dir.mkdir( baseDirectory() );
		}
		else
		{
			setResult( QDialog::Rejected );
			return;
		}
	}

	if( gameName().isEmpty() )
	{
		QDialog::reject();
		return;
	}
	QDialog::accept();
}


void CreateProjectDialog::reject()
{
	DLOG( INFO ) << "Reject";
	QDialog::reject();
}
